<template>
  <t-modal
    destroyOnClose
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="loading"
    @ok="
      () => {
        $emit('ok');
      }
    "
    @cancel="
      () => {
        $emit('cancel');
      }
    "
  >
    <a-spin :spinning="loading">
      <a-alert type="warning" banner>
        <a slot="description" @click="openDoc()">点击查看使用文档</a>
      </a-alert>
      <a-form :form="form">
        <a-form-item class="hiddenItem">
          <a-input v-decorator="['uuid']" type="hidden" />
        </a-form-item>
        <a-form-item class="hiddenItem">
          <a-input v-decorator="['configType']" type="hidden" />
        </a-form-item>
        <a-form-item
          label="企业号id"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-input-password
            placeholder="请输入企业号id"
            v-decorator="[
              'agentId',
              { rules: [{ required: true, message: '请输入企业号id' }] },
            ]"
          />
        </a-form-item>
        <a-form-item
          label="应用凭证"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-input-password
            placeholder="请输入应用凭证"
            v-decorator="[
              'appKey',
              { rules: [{ required: true, message: '请输入应用凭证' }] },
            ]"
          />
        </a-form-item>
        <a-form-item
          label="凭证密码"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-input-password
            placeholder="请输入凭证密码"
            v-decorator="[
              'appSecret',
              { rules: [{ required: true, message: '请输入凭证密码' }] },
            ]"
          />
        </a-form-item>
      </a-form>
    </a-spin>
  </t-modal>
</template>

<script>
import pick from "lodash.pick";
// 表单字段
const fields = [
  "uuid",
  "configType",
  "agentId",
  "appKey",
  "appSecret",
  "orgOperateType",
  "usrOperateType",
];
export default {
  props: {
    visible: {
      type: Boolean,
      required: true,
    },
    loading: {
      type: Boolean,
      default: () => false,
    },
    model: {
      type: Object,
      default: () => null,
    },
    type: {
      type: String,
    },
  },
  data() {
    return {
      title: "",
      labelCol: {
        xs: {
          span: 24,
        },
        sm: {
          span: 3,
        },
      },
      wrapperCol: {
        xs: {
          span: 24,
        },
        sm: {
          span: 20,
        },
      },
      form: this.$form.createForm(this),
      isUserPopups: false,
      valueField: "uuid",
    };
  },
  methods: {
    openDoc() {
      window.open(
        "https://www..com/docs/disanfangyingyong/alidingding/",
        "_blank"
      );
    },
    setType() {
      this.$nextTick(() => {
        this.form.setFieldsValue({
          configType: this.type,
        });
      });
    },
    add(m) {
      console.log(m);
      this.$nextTick(() => {
        this.form.setFieldsValue({
          puuid: m.puuid,
          code: m.code,
        });
      });
    },
    edit(m) {
      const booleanFields = ["orgOperateType", "usrOperateType"];
      booleanFields.forEach((field) => {
        m[field] = m[field] === "true";
      });
      this.$nextTick(() => {
        this.form.setFieldsValue(pick(m, fields));
      });
    },
  },
  created() {
    // 防止表单未注册
    fields.forEach((v) => this.form.getFieldDecorator(v));
  },
};
</script>
<style lang="less" scoped>
/deep/.ant-alert-with-description.ant-alert-no-icon {
  padding: 5px 5px 5px 20px;
  margin: 0 32px 10px 20px;
}
</style>
